package Websites;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import KUtilities.KLogger;

public class Way2SMS extends SMSService{

	private String action;

    public Way2SMS(KLogger logger){
    	super(logger,"site5.way2sms.com");
    	setSendData("http://site5.way2sms.com/quicksms.action","http://site5.way2sms.com/jsp/InstantSMS.jsp","Message has been submitted successfully");
    	setLoginData("http://site5.way2sms.com/Login1.action","http://site5.way2sms.com/entry.jsp");
    }
    
	protected String getLoginContent(String username, String password) {
		String loginContent;
		loginContent="username="+username;
		loginContent+="&password="+password;
		loginContent+="&button=Login";
		return loginContent;
	}
	
	protected boolean isValidLogin(HttpURLConnection loginConnection){
		if(loginConnection.getHeaderField("Location").indexOf("Main.action;jsessionid=")==-1)
			return false;
		return true;
	}
	
	protected void grabRequirements(){
		thread=new Thread(){
        	public void run(){
        		try{
        			URL location=new URL("http://site5.way2sms.com/jsp/InstantSMS.jsp");
                    HttpURLConnection actionConnection=(HttpURLConnection)location.openConnection();
                    
                    actionConnection.setRequestProperty("Host",host);
                    actionConnection.setRequestProperty("User-Agent","Mozilla/5.0 (X11; Linux x86_64; rv:7.0.1) Gecko/20100101 Firefox/7.0.1");
                    actionConnection.setRequestProperty("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
                    actionConnection.setRequestProperty("Accept-Language","en-us,en;q=0.5");
                    actionConnection.setRequestProperty("Accept-Charset","ISO-8859-1,utf-8;q=0.7,*;q=0.7");
                    actionConnection.setRequestProperty("Connection","keep-alive");
                    actionConnection.setRequestProperty("Cookie", cookie);
                    actionConnection.setRequestMethod("GET");
                    actionConnection.setInstanceFollowRedirects(false);
                    	                    
                    BufferedReader actionReader=new BufferedReader(new InputStreamReader(actionConnection.getInputStream()));
                    StringBuilder response =new StringBuilder();
                    String temp="";
                    while((temp=actionReader.readLine())!=null){
                        response.append(temp+"\n");
                    }
                    actionReader.close();
                    String searchStr="<input type=\"hidden\" name=\"Action\" id=\"Action\" value=";
                    int i=response.indexOf(searchStr)+searchStr.length()+1;
                    action=response.substring(i,response.indexOf("\"", i));
        		}catch(Exception e){}
        	}
        };
        thread.start();
	}
	
    protected String getSendContent(String number,String message)
    {
    	String sendContent;
    	sendContent="HiddenAction=instantsms";
    	sendContent+="&Action="+action;
    	sendContent+="&MobNo="+ number;
    	sendContent+="&textArea="+message;
        return sendContent;
    }
        
    public boolean logout()
    {
    	return logout("http://site5.way2sms.com/LogOut");
    }
    
	public String getServiceName() {
		return "Way2SMS";
	}

	public int getMaxChar() {
		return 140;
	}	
}
